<?php

namespace App\Repositories;

use Storage;
use App\Models\Picture;
use App\Models\PictureMapping;

class PictureMappingRepository
{
    /**
     * 文件列表(搜索)
     * @param  [type] $request [description]
     * @return [type]          [description]
     */
    public function getMappingBySearch($request, $type = false)
    {
        $model = PictureMapping::select([
            'picture_mapping.id',
            'picture_mapping.picture_id',
            'picture_mapping.summary',
            'picture_mapping.title',
            'picture_mapping.link',
            'picture.path',
        ])
            ->leftJoin('picture', 'picture_mapping.picture_id', '=', 'picture.id')
            ->where(function ($query) use ($request) {
                $query->when(isset($request['object_type']) && !empty($request['object_type']), function ($query) use ($request) {
                    return $query->where('picture_mapping.object_type', $request['object_type']);
                })
                    ->when(isset($request['object_id']) && !empty($request['object_id']), function ($query) use ($request) {
                        if (is_array($request['object_id'])) {
                            return $query->whereIn('picture_mapping.object_id', $request['object_id']);
                        } else {
                            return $query->where('picture_mapping.object_id', intval($request['object_id']));
                        }
                    });
            })->orderBy('picture_mapping.sequence', 'asc');

        if ($type) {
            return $model->pluck("picture.path")->toArray();
        }

        return $model->get()->toArray();
    }

    /**
     * 文件列表(搜索)
     * @param  [type] $request [description]
     * @return [type]          [description]
     */
    public function getMappings($request, $type = false)
    {
        $model = PictureMapping::query()->join('picture', 'picture_mapping.picture_id', '=', 'picture.id')
            ->where(function ($query) use ($request) {
                $query->when(isset($request['object_type']) && !empty($request['object_type']), function ($query) use ($request) {
                    return $query->where('picture_mapping.object_type', $request['object_type']);
                })
                    ->when(isset($request['object_id']) && !empty($request['object_id']), function ($query) use ($request) {
                        if (is_array($request['object_id'])) {
                            return $query->whereIn('picture_mapping.object_id', $request['object_id']);
                        } else {
                            return $query->where('picture_mapping.object_id', intval($request['object_id']));
                        }
                    });
            })
            ->orderBy('picture_mapping.sequence', 'asc');

        if ($type) {
            return $model->pluck("picture.path")->toArray();
        }

        return $model->get()->toArray();
    }

    public function updateMapping($object_id, $object_type = 'content', $picture_ids = [])
    {
        if (!$picture_ids) return;

        PictureMapping::where([
            'object_type' => $object_type,
            'object_id' => $object_id,
        ])->delete();

        $seq = 1;
        foreach ($picture_ids as $k => $picture_id) {
            $data['object_id'] = $object_id;
            $data['object_type'] = $object_type;
            $data['picture_id'] = $picture_id;
            $data['sequence'] = ++$seq;
            PictureMapping::insert($data);
        }

        return true;
    }

    public function updateSimpleMapping($id = null, $object_id, $object_type = 'content', $picture_id = null)
    {
        if (empty($picture_id)) return;
        if (!empty($id)) {
            return PictureMapping::where("id", $id)->update(['picture_id' => $picture_id,]);
        }
        $seq = PictureMapping::where(['object_id' => $object_id, 'object_type' => $object_type])->count("id");
        $res = PictureMapping::create([
            'object_id' => $object_id,
            'object_type' => $object_type,
            'picture_id' => $picture_id,
            'sequence' => $seq ? ($seq + 1) : 1,
        ]);

        return $res['id'] ?? "";
    }

    public function getMapping($filter = [])
    {
        return PictureMapping::where($filter)->first();
    }

    public function deletePicture(int $id)
    {
        if (!$id) return "";
        return PictureMapping::where("id", "=", $id)->delete();
    }

    /**
     * 文件列表(搜索)
     * @param  [type] $request [description]
     * @return [type]          [description]
     */
    public
    function getMappingBySearchFiter($request)
    {
        return PictureMapping::query()
            ->leftJoin('picture', 'picture_mapping.picture_id', '=', 'picture.id')
            ->where(function ($query) use ($request) {
                $query->when(isset($request['object_type']) && !empty($request['object_type']), function ($query) use ($request) {
                    return $query->where('picture_mapping.object_type', $request['object_type']);
                })
                    ->when(isset($request['object_id']) && !empty($request['object_id']), function ($query) use ($request) {
                        if (is_array($request['object_id'])) {
                            return $query->whereIn('picture_mapping.object_id', $request['object_id']);
                        } else {
                            return $query->where('picture_mapping.object_id', intval($request['object_id']));
                        }
                    });
            })->orderBy('picture_mapping.sequence', 'asc')
            ->pluck("path")
            ->toArray();
    }


}
